package com.cxb.demotest.mq.rocketmq.scheduled_message;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;


/**
 * 延时消息生产者
 * @author Xb
 */
public class ScheduledProducer {
    public static void main(String[] args) throws Exception {
        // 实例化消息生产者
        DefaultMQProducer producer = new DefaultMQProducer("ScheduledProducer");

        // 设置nameserver的地址
        producer.setNamesrvAddr("127.0.0.1:9876");

        // 启动producer实例
        producer.start();
        int totalMessagesToSend = 10;
        for (int i = 0; i < totalMessagesToSend; i++) {
            final int index = i;
            // 创建消息，执行topic，tag，消息体
            Message message = new Message(
                    "ScheduledTopic",
                    ("Hello, scheduled message!" + i).getBytes()
            );
            // 设置延时等级3，消息将会在10s之后投递给消费者
            // delayTimeLevel：（1-18个等级）：1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
            message.setDelayTimeLevel(3);

            producer.send(message);
        }

        // 不再发送消息，关闭生产者实例
        producer.shutdown();

    }

}
